package tg.edu.fhh.demo.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import tg.edu.fhh.demo.entity.Student;
import tg.edu.fhh.demo.entity.SysRole;
import tg.edu.fhh.demo.entity.TbClass;
import tg.edu.fhh.demo.repository.StudentRepository;
import tg.edu.fhh.demo.service.StudentService;
import tg.edu.fhh.demo.service.SysRoleService;
import tg.edu.fhh.demo.service.TbClassService;
import tg.edu.fhh.demo.web.form.StudentForm;

@Controller
@RequestMapping(value = "/student")
public class StudentController {
	
	@Autowired
	private StudentService studentService;
	@Autowired
	private StudentRepository stuDAO;
	
	
	@Autowired
	private TbClassService tbClassService;
	@Autowired
	private SysRoleService sysRoleservice;
	
	@RequestMapping(value = "/manage")
	public void manage(ModelMap modelMap) {
		List<TbClass> list=tbClassService.findAll();
		modelMap.put("bjs", list);
	}
	
	@RequestMapping(value = "/table")
	public String table(Integer bjId,ModelMap modelMap) {
		List<Student> list=studentService.findByTbClassId(bjId);
			modelMap.put("list", list);
		return "student/table";
		
	}
	

	@RequestMapping(value = "/edit2")
	public void edit2( Integer bjId,ModelMap modelMap) {
		Student model=new Student();
		TbClass tbClass=tbClassService.findById(bjId);
		model.setTbClass(tbClass);
		modelMap.put("model", model);
	}
	
	@RequestMapping(value = "/update")
	public void update(Integer id,ModelMap modelMap) {
		Student model=new Student();
		
		model=studentService.findById(id);
	modelMap.put("model", model);
	
	}
	
	@RequestMapping(value = "/refreshpwd")
	public void refreshpwd(Integer id) {
		Student s=studentService.findById(id);
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		String password =encoder.encode("1111");
		s.setPassword(password);
		studentService.save(s);
	}
	
	
	@RequestMapping(value = "/save")
	@ResponseBody
	public Object save(StudentForm form ) {
		Student model=new Student();
		Integer id=form.getId();
		if(id!=null) {
			model=studentService.findById(id);
			BeanUtils.copyProperties(form, model);
			model.setUsername(form.getStuNo());
			studentService.save(model);
			return "ok";
		}else {
			
			BeanUtils.copyProperties(form,model);
			Integer bjId=form.getTbClassId();
			model.setTbClass(tbClassService.findById(bjId));
			model.setUsername(form.getStuNo());
			List<SysRole> role=new ArrayList<SysRole>();
			role.add(sysRoleservice.findById(3));
			model.setRoles(role);
			BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
			String password=encoder.encode(form.getPassword1());
			model.setPassword(password);
			studentService.save(model);
			return "ok";
		}
	}
	
	
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Object delete(Integer id) {
		studentService.deleteById(id);
		return "ok";
		}
	
	
	@RequestMapping(value = "/search",method = RequestMethod.GET)
	public void search(StudentForm form,ModelMap modelMap) {
		List<Student> list = studentService.findAll();
		modelMap.put("list", list);
		modelMap.put("model", form);
	}
	
}
